/**
 * Created by justin on 01/11/2013.
 */

var SerialPort = require("serialport");
var express = require('express');
var app = express();

exports.switch = function(req, res){
    var code = req.query.code;
    console.log('code : ' + code);
    send2Arduino(code);
    res.redirect('/');
    res.end();
};


var send2Arduino = function(data){

    console.log("SENDING : "+data);

    var serialPort = new SerialPort.SerialPort("/dev/tty.usbserial-A7027J8V", {
        baudrate: 9600,
        dataBits: 8,
        parity: 'none',
        stopBits: 1,
        flowControl: false
    });

    serialPort.on('open',function() {
        console.log('Port open');
        var senddata = new Buffer(data);
        serialPort.write(senddata);
    });

    serialPort.on('data', function(indata) {
        console.log("Data : "+indata.toString());
    });
};